<?php  

require_once($CFG->libdir .'/rsslib.php');

function generate_exercise_feed($user,$feed_action,$feed_title,$exercise_records,$return_asString = false) {
	
	global $CFG,$USER;
	
	$feedurl = "{$CFG->wwwroot}/mercurius/{$CFG->gwtmodule}/index.php?action=$feed_action&feedkey={$user->feed_key}";
	
	$articles = "";

	//First all rss feeds common headers
	$header = rss_standard_header(strip_tags($feed_title),$feedurl);
	
	//Now all the rss items
	if (!empty($header)) {
		$lastupdate = "0";
		$items = array();
		foreach($exercise_records as $index => $log) {
			if(!empty($log->session_prkey)) { // Is session ID, retrieve log
				$log = get_record('mercurius_exercise_log','id',$log->session_prkey);
			}
	
			clean_exercise_log($log);
			$exercise_action = "";
			$has_map_sessions = has_map_sessions($log->id);
			if($has_map_sessions) {
				$exercise_action = "generateGMap";
			} else {
				$exercise_action = "viewExercise";
			}
			
			if($log->datetime == '0000-00-00 00:00:00') {
				$dt = strtotime($log->date);
			} else {
				$dt = strtotime($log->datetime);
			}
			if($dt > $lastupdate) {
				$lastupdate = $dt;	
			}
			
			$dt = date('U', $dt);
	
			$summary = generate_summary($log,$exercise_action,$has_map_sessions);
			$content = generate_content($log,$exercise_action,$has_map_sessions);
	
			$url = "{$CFG->wwwroot}/mercurius/{$CFG->gwtmodule}/index.php?action=$exercise_action&id={$log->id}&feedkey={$user->feed_key}";	
			if(empty($firsturl)) {
				$firsturl = $url;
			}
			
			$item = new stdClass();
			$item->title = $log->title;
			$item->author = fullname($user,true);
			$item->pubdate = $dt;
			//$item->pubdate = date("U");
			if(!empty($exercise_action)) {
				$item->link = $url;
			}
			$item->description = $summary;
			
			$items[] = $item;
		}

		//Now, if items, we begin building the structure
		if (!empty($items)) {
			$articles = rss_add_items($items);
		}

	}
	
	//Now all rss feeds common footers
	if (!empty($header) && !empty($articles)) {
		$footer = rss_standard_footer();
	}
	
	//Now, if everything is ok, concatenate it
	if (!empty($header) && !empty($articles) && !empty($footer)) {
		$articles = $header.$articles.$footer;
	} else {
		$articles = false;
	}
	
	if($return_asString) {
		return $articles;
	} else {
		//header('Content-type: ' . self::OUTPUT_MIMETYPE . ';charset='UTF-8 \r\n');
		//header('Content-Disposition: inline; filename=feed.xml');
		echo $articles;
	}	
}


function generate_gadget_feed($return_asString = false) {
	
	global $CFG,$USER;
	
	$feedurl = "{$CFG->wwwroot}/mercurius/{$CFG->gwtmodule}/index.php?action=gadgetFeed&feedkey={$USER->feed_key}";

	$articles = "";

	//First all rss feeds common headers
	$header = rss_standard_header(strip_tags("Mercurius Gadget Applications"),$feedurl);
	//Now all the rss items
	if (!empty($header)) {
	
		$lastupdate = "0";
		$items = array();
		$dirs = get_directory_list($CFG->dirroot . '/mercurius/gadgets', '', false,true,false);
		$items = array();
		foreach($dirs as $index => $dir) {
			
			$xmlfile = $CFG->dirroot . '/mercurius/gadgets/' . $dir . '/' . $dir .'.xml';
			 if (file_exists($xmlfile)) {
				$dt = date('U');
				$url = $xmlfile;	
	
				$xml = simplexml_load_file($xmlfile);
	
				$title = (string) $xml->ModulePrefs['directory_title'];
				$desc = (string) $xml->ModulePrefs['description'];
				$author = (string) $xml->ModulePrefs['author'];
				$thumbnail = (string) $xml->ModulePrefs['thumbnail'];
	
				$content = "" .
					"<table width='100%''><tr><td width='115px' align='left'>" .
					"<img src='$thumbnail' width='150px' border='0'>" .
					"</td><td align='left' valign='top'><table height='120px'>" .
					"<tr><td valign='top'><b>Title:</b> $title</td></tr>" .
					"<tr><td valign='top'><b>Description:</b> $desc</td></tr>" .
					"<tr><td><hr/></td></tr>" .
					"<tr>" .
					"<td valign='bottom'>" .
					"<a href='http://fusion.google.com/add?moduleurl={$CFG->wwwroot}/mercurius/gadgets/$dir/$dir.xml'>" .
					"<img target='_blank' src='{$CFG->wwwroot}/mercurius/gadgets/addgoogle.gif' border='0' alt='Add to Google'>" .
					"</a>" .
					"</td>" .
					"</tr>" .
					"</table>" .
					"</td>" .
					"</tr>" .
					"</table><hr/>";
			
			
			
				$item = new stdClass();
				$item->title = $title;
				$item->author = $author;
				$item->pubdate = $dt;
				//$item->pubdate = date("U");
				$item->link = $url;
				//$item->description = $desc;
				$item->description = $content;
			
				$items[] = $item;
			}
		}

		//Now, if items, we begin building the structure
		if (!empty($items)) {
			$articles = rss_add_items($items);
		}

	}
	//Now all rss feeds common footers
	if (!empty($header) && !empty($articles)) {
		$footer = rss_standard_footer();
	}
	
	//Now, if everything is ok, concatenate it
	if (!empty($header) && !empty($articles) && !empty($footer)) {
		$articles = $header.$articles.$footer;
	} else {
		$articles = false;
	}
	
	if($return_asString) {
		return $articles;
	} else {
		//header('Content-type: ' . self::OUTPUT_MIMETYPE . ';charset='UTF-8 \r\n');
		//header('Content-Disposition: inline; filename=feed.xml');
		echo $articles;
	}	
}
?>
